package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.myapplication.Adapter.NodepadListAdapter;
import com.example.myapplication.Bean.Notepad;
import com.example.myapplication.databinding.ActivityNodeworkBinding;
import com.example.myapplication.db.MyDbHelper;
import com.example.myapplication.ui.EditActivity;

import java.util.List;

public class nodework extends AppCompatActivity {

    private ActivityNodeworkBinding binding;
    private  NodepadListAdapter nodepadListAdapter;
    public MyDbHelper myDbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityNodeworkBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        myDbHelper = new MyDbHelper(this);
        //点击新增  跳转编辑页面
//        binding.add.setOnClickListener(v -> startActivity(new Intent(nodework.this, EditActivity.class)));

        binding.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(nodework.this, EditActivity.class),100);
            }
        });
         nodepadListAdapter = new NodepadListAdapter();
        //增加分割线
        binding.List.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        binding.List.setAdapter(nodepadListAdapter);
        binding.List.setLayoutManager(new LinearLayoutManager(this));

        findAll();
    }
    //当页面在显示的时候更新记事本内容
//    @Override
//    protected void onResume(){
//
//        super.onResume();
//        findAll();
//    }

    public void findAll() {
        List<Notepad> notepadList = myDbHelper.findAll();
        nodepadListAdapter.update(notepadList);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if(requestCode==100 && resultCode == RESULT_OK){
                findAll();
            }
    }
}